#include "testsearchwidget.h"
#include "ui_testsearchwidget.h"
#include "iconhelper.h"
#include "databaseoperate.h"

testSearchWidget::testSearchWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::testSearchWidget)
{
    ui->setupUi(this);
    calenderWidget = new QCalendarWidget;
    calenderWidget->setWindowFlag(Qt::FramelessWindowHint);
    calenderWidget->setAttribute(Qt::WA_ShowModal);
    connect(calenderWidget, SIGNAL(clicked(QDate)), this, SLOT(setDate(QDate)));
    IconHelper::Instance()->setIcon(ui->pushButton_2, QChar(0xf073), 12);
    IconHelper::Instance()->setIcon(ui->pushButton_3, QChar(0xf00D), 13);
    connect(ui->pushButton_3, SIGNAL(clicked()), this, SLOT(clearDateLineEdit()));
//    initTableWidget();
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);//设置选中模式为选中行
    initCombox();
}

testSearchWidget::~testSearchWidget()
{
    delete ui;
}

void testSearchWidget::on_pushButton_2_clicked()
{

    calenderWidget->show();
}
void testSearchWidget::setDate(QDate dateTime)
{
    ui->lineEdit->setText(dateTime.toString("yyyy-MM-dd"));
    calenderWidget->hide();
}
void testSearchWidget::clearDateLineEdit()
{
    ui->lineEdit->clear();
}
void testSearchWidget::initTableWidget()
{
    TestAttrList c;
    databaseOperate::Instance()->queryTestInfo(c);

    int i = 0;
    foreach (TestAttr tA, c) {
        ui->tableWidget->setRowCount(i+1);
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(tA.at(0)));
        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(tA.at(1)));
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(tA.at(2)));
        ui->tableWidget->setItem(i, 3, new QTableWidgetItem(tA.at(3)));
        ui->tableWidget->setItem(i, 4, new QTableWidgetItem(tA.at(4)));

        ui->tableWidget->setItem(i, 5, new QTableWidgetItem(tA.at(5)));
        ui->tableWidget->setItem(i, 6, new QTableWidgetItem(tA.at(6)));
        ui->tableWidget->setItem(i, 7, new QTableWidgetItem(tA.at(7)));
        ui->tableWidget->setItem(i, 8, new QTableWidgetItem(tA.at(8)));
        i++;
    }

}

void testSearchWidget::on_pushButton_clicked()
{
    ui->tableWidget->clearContents();
    TestAttrList c;
    Condition cond;
    cond.band = ui->comboBox->currentText();
    if(cond.band == "请选择")
        cond.band = "";
    cond.date = ui->lineEdit->text();
    cond.shifts = ui->comboBox_2->currentText();
    if(cond.shifts == "请选择")
        cond.shifts = "";
    cond.suveyor = ui->comboBox_3->currentText();
    if(cond.suveyor == "请选择")
        cond.suveyor = "";
    cond.number = ui->lineEdit_9->text();
    databaseOperate::Instance()->queryCondition(cond, c);
    int i = 0;
    foreach (TestAttr tA, c) {
        ui->tableWidget->setRowCount(i+1);
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(tA.at(0)));
        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(tA.at(1)));
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(tA.at(2)));
        ui->tableWidget->setItem(i, 3, new QTableWidgetItem(tA.at(3)));
        ui->tableWidget->setItem(i, 4, new QTableWidgetItem(tA.at(4)));

        ui->tableWidget->setItem(i, 5, new QTableWidgetItem(tA.at(5)));
        ui->tableWidget->setItem(i, 6, new QTableWidgetItem(tA.at(6)));
        ui->tableWidget->setItem(i, 7, new QTableWidgetItem(tA.at(7)));
        ui->tableWidget->setItem(i, 8, new QTableWidgetItem(tA.at(8)));
        ui->tableWidget->setItem(i, 9, new QTableWidgetItem(tA.at(9)));
        i++;
    }
    ui->tableWidget->setColumnWidth(0, 145);
}

void testSearchWidget::initCombox()
{
    QList<QString> list;
    list.clear();
    databaseOperate::Instance()->queryTestShiftInfo(list);
    foreach(QString shift, list)
    {
        if(shift == "")
            continue;
        ui->comboBox_2->addItem(shift);
    }
    list.clear();
    databaseOperate::Instance()->queryTestSampleInfo(list);
    foreach(QString sample, list)
    {
        if(sample == "")
            continue;
        ui->comboBox->addItem(sample);
    }
    list.clear();
    databaseOperate::Instance()->queryTestSurveyorInfo(list);
    foreach(QString sur, list)
    {
        if(sur == "")
            continue;
        ui->comboBox_3->addItem(sur);
    }
}

void testSearchWidget::on_pushButton_5_clicked()
{
    QMessageBox::StandardButton result= QMessageBox::information(NULL, "提示", "确认删除？",QMessageBox::Yes|QMessageBox::No);
    switch (result)
    {
    case QMessageBox::Yes:
        break;
    case QMessageBox::No:
        return;
    default:
        return;
        break;
    }
    QList<int> numList;
    QList<QTableWidgetItem*> items = ui->tableWidget->selectedItems();
    foreach(QTableWidgetItem* item, items)
    {
        if(item->column() != 0)
            continue;
        numList.append(item->data(0).toInt());
    }
    databaseOperate::Instance()->deleteTestInfo(numList);
    on_pushButton_clicked();
}

void testSearchWidget::saveTabel2Excel()
{
    QString dataTimeStr = QDateTime::currentDateTime().toString("yyyy-MM-dd -hh-mm-ss").remove(QRegExp("\\s"));;
    QString fileName = QFileDialog::getSaveFileName(this, tr("Excel file"), QString("./" +dataTimeStr+".xls"), tr("Excel Files(*.xls)"));//设置保存的文件名
    if(fileName != "")
    {
        QAxObject *excel = new QAxObject;
        if(excel->setControl("Excel.Application"))
        {
            excel->dynamicCall("SetVisible (bool Visible)", false);
            excel->setProperty("DisplayAlerts", false);
            QAxObject *workbooks = excel->querySubObject("WorkBooks");      //获取工作簿集合
            workbooks->dynamicCall("Add");                                  //新建一个工作簿
            QAxObject *workbook = excel->querySubObject("ActiveWorkBook");  //获取当前工作簿
            QAxObject *worksheet = workbook->querySubObject("Worksheets(int)", 1);
            QAxObject *cell;
            //添加Excel表头数据
            for (int i = 1; i <= ui->tableWidget->columnCount(); i++)
            {
                cell = worksheet->querySubObject("Cells(int, int)", 1, i);
                cell->setProperty("RowHeight", 40);
                cell->setProperty("RowWidth", 150);
                cell->dynamicCall("SetValue(const QString&)", ui->tableWidget->horizontalHeaderItem(i-1)->data(0).toString());
            }
            //将form列表中的数据依次保存到Excel文件中
            for(int i=0;i<ui->tableWidget->rowCount();i++){
                for (int j=0;j<ui->tableWidget->columnCount();j++)
                {
                    cell = worksheet->querySubObject("Cells(int,int)", i+2, j+1);
                    cell->dynamicCall("SetValue(const QString&)", ui->tableWidget->item(i,j)?ui->tableWidget->item(i,j)->text():"");
                }
            }
            //将生成的Excel文件保存到指定目录下
            workbook->dynamicCall("SaveAs(const QString&)", QDir::toNativeSeparators(fileName));//保存至fileName
            workbook->dynamicCall("Close()");
            excel->dynamicCall("Quit()");
            delete excel;
            excel = NULL;
            if(QMessageBox::question(NULL, QString::fromUtf8("完成"), QString::fromUtf8("文件已经导出，是否现在打开?"), QMessageBox::Yes|QMessageBox::No) == QMessageBox::Yes)
            {
                QDesktopServices::openUrl(QUrl("file:///" + QDir::toNativeSeparators(fileName)));
            }
        }
    }
}


void testSearchWidget::on_pushButton_6_clicked()
{
    saveTabel2Excel();
}
